package com.njiandan.laosijivideo.plugin;

import android.app.Activity;
import android.content.Intent;

import com.njiandan.laosijivideo.BarcodeScannerActivity;
import com.njiandan.laosijivideo.WebDetailActivity;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;


public class QrScanPlugin implements MethodChannel.MethodCallHandler,
        PluginRegistry.ActivityResultListener,
        PluginRegistry.RequestPermissionsResultListener {
    private static final String CHANNEL = "com.njiandan.laosijivideo/video";

    private final PluginRegistry.Registrar registrar;

    private MethodChannel.Result pendingResult;
    private MethodCall methodCall;

    private QrScanPlugin(PluginRegistry.Registrar registrar) {
        this.registrar = registrar;
    }


    public static void registerWith(PluginRegistry.Registrar registrar) {
        final MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL);
        final QrScanPlugin instance = new QrScanPlugin(registrar);
        registrar.addActivityResultListener(instance);
        registrar.addRequestPermissionsResultListener(instance);
        channel.setMethodCallHandler(instance);
    }

    @Override
    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
        Activity activity = registrar.activity();

        pendingResult = result;
        methodCall = call;

        switch (call.method) {
            case "scan": {
                Intent intent = new Intent(activity, BarcodeScannerActivity.class);
                activity.startActivityForResult(intent, 100);
                break;
            }
            case "openWebviewVideo": {
                String videoUrl = call.argument("videoUrl");
                String videoTitle = call.argument("videoTitle");
                String webviewUrl = call.argument("webviewUrl");
                Intent intent = new Intent(activity, WebDetailActivity.class);
                intent.putExtra(WebDetailActivity.VIDEO_URL, videoUrl);
                intent.putExtra(WebDetailActivity.VIDEO_WEBVIEW_URL, webviewUrl);
                intent.putExtra(WebDetailActivity.VIDEO_TITLE, videoTitle);
                activity.startActivity(intent);
                result.success("");
                break;
            }
            default:
                throw new IllegalArgumentException("Unknown method " + call.method);
        }
    }

    @Override
    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null && requestCode == 100) {
            if (resultCode == Activity.RESULT_OK) {
                String barcode = data.hasExtra("SCAN_RESULT") ? data.getStringExtra("SCAN_RESULT") : "";
                pendingResult.success(barcode);
            } else {
                String errorCode = data.hasExtra("ERROR_CODE") ? data.getStringExtra("ERROR_CODE") : "";
                pendingResult.error(errorCode, null, null);
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean onRequestPermissionsResult(int i, String[] strings, int[] ints) {
        return false;
    }
}
